package it.maggioni.sandbox.hibernate4;

import it.maggioni.sandbox.hibernate4.dto.UserDetails03;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Ex03 {
	
	/**
	 * Ora sperimentiamo: uso dei transient (campi non salvati nel db),
	 * salvataggio della sola data e non anche dell'ora, i LOB (large object)
	 * @param args
	 */
	
	public static void main(String[] args) {
		UserDetails03 userDetails = new UserDetails03();
		userDetails.setUserId(1);
		userDetails.setUserName("First User");
		userDetails.setAddress("First User's address");
		userDetails.setJoinedDate(new Date());
		userDetails.setJoinedDateWithoutTime(userDetails.getJoinedDate());
		userDetails.setDescription("First User's description");
		userDetails.setDoNotSaveInDb("Qesto non deve essere salvato nel db");
		
		Configuration configuration = new Configuration().configure("/"+Ex03.class.getPackage().getName().replace(".","/")+"/hibernate-"+Ex03.class.getSimpleName()+".cfg.xml");
	    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
	    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	    
	    Session session = sessionFactory.openSession();
	    session.beginTransaction();
	    session.save(userDetails);
	    session.getTransaction().commit();
	    session.close();
	    
	    sessionFactory.close();
	}

}
